import os
import yaml


# 读取  f就是文件对象
def read_yaml(key):
    try:
        with open(os.getcwd() + '/extract.yaml', mode='r', encoding='utf-8') as f:
            value = yaml.load(f, yaml.FullLoader)
            return value[key]
    except Exception as e:
        return e


# 写入
def write_yaml(data):
    try:
        with open(os.getcwd() + '/extract.yaml', mode='a', encoding='utf-8') as f:
            yaml.dump(data, stream=f, allow_unicode=True)
    except Exception as e:
        return e


# 清空
def clear_yaml():
    try:
        with open(os.getcwd() + '/extract.yaml', mode='w', encoding='utf-8') as f:
            f.truncate()
    except Exception as e:
        return e


# 读取测试用例
def read_case_yaml(yaml_name):
    # param case_path: yaml文件路径
    # return: 返回的是python数据类型的数据
    try:
        with open(os.getcwd() + '/case_data/' + yaml_name, mode='r', encoding="utf-8") as f:
            result = yaml.load(stream=f, Loader=yaml.FullLoader)
            return result
    except Exception as e:
        return e


